package club.terroir.client.producteur;



import club.terroir.client.Club_terroir;
import club.terroir.client.ControlerClubAdmin;

import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.Grid;
import com.google.gwt.user.client.ui.HorizontalPanel;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.TextBox;
import com.google.gwt.user.client.ui.VerticalPanel;


public class ContactCreateGUI extends VerticalPanel{

	public static ContactCreateGUI instance = new ContactCreateGUI();
	Button buttonNouveauClient = new Button("Créer Nouveau Client");
	TextBox textBoxName = new TextBox();
	TextBox textBoxFirstName = new TextBox();
	TextBox textBoxTelephone = new TextBox();
	TextBox textBoxEmail = new TextBox();
	TextBox textBoxAddress1 = new TextBox();
	TextBox textBoxAddress2 = new TextBox();
	TextBox textBoxVille = new TextBox();
	TextBox textBoxCodePostal = new TextBox();
	Label label = new Label();
	Grid grid ;
	
	private ContactCreateGUI() {
		super();
		this.add(new Label("Créer un nouveau client"));
		grid = new Grid(8, 2);
		//grid.setBorderWidth(1);
		addToGrid(0,"Name",textBoxName);
		addToGrid(1,"Prénom",textBoxFirstName);
		addToGrid(2,"Téléphone",textBoxTelephone);
		addToGrid(3,"Email",textBoxEmail);
		addToGrid(4,"Adresse 1",textBoxAddress1);
		addToGrid(5,"Adresse 2",textBoxAddress2);
		addToGrid(6,"Ville",textBoxVille);
		addToGrid(7,"Code Postal",textBoxCodePostal);
		this.add(grid);
		HorizontalPanel panelButton = new HorizontalPanel();
		panelButton.add(buttonNouveauClient);
		this.add(panelButton);
		this.add(label);
		
		this.buttonNouveauClient.addClickHandler(new ClickHandler() {			
			@Override
			public void onClick(ClickEvent event) {
				sauvegarderClient();
			}
		});
	}
	
	private void addToGrid(int row ,String s, TextBox textBox) {
		 grid.setText(row, 0, s);
		 grid.setWidget(row, 1, textBox);
	}

	private void sauvegarderClient() {
		String name = this.textBoxName.getText();
		String firstName = this.textBoxFirstName.getText();
		String telephone = this.textBoxTelephone.getText();
		String email = this.textBoxEmail.getText();
		String address1 = this.textBoxAddress1.getText();
		String address2 = this.textBoxAddress2.getText();
		String ville = this.textBoxVille.getText();
		String codePostal = this.textBoxCodePostal.getText();
		Contact contact = new Contact(null,name, firstName, telephone, email, address1, address2, codePostal, ville);
		sauvegarderClient(contact);
		
	}

	private void sauvegarderClient(Contact contact) {
		this.buttonNouveauClient.setEnabled(false);
		Club_terroir.greetingService.create(contact, new AsyncCallback<Contact>() {			
			@Override
			public void onSuccess(Contact result) {
				label.setText("sauvegarde effectuée");
				ContactsListGUI.instance.addContactToList(result);
				//ControlerClubAdmin.instance.displayListeClients();
				ControlerClubAdmin.instance.achatCreateGUI.setContactInfo(result);
			}			
			@Override
			public void onFailure(Throwable e) {
				label.setText("Exception "+e.getMessage());
				Window.alert("Exception sauvegarde Contact "+e);
			}
		});
		this.buttonNouveauClient.setEnabled(true);
	}
	
	
}
